package net.lqy.aipan.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.lqy.aipan.dto.AccountFileDTO;
import net.lqy.aipan.dto.FolderTreeNodeDTO;
import net.lqy.aipan.interceptor.LoginInterceptor;
import net.lqy.aipan.request.*;
import net.lqy.aipan.service.AccountFileService;
import net.lqy.aipan.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @title: FileController
 * @Author lqy
 * @Date: 2025-06-04 20:10
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1/account_file")
@Tag(name = "用户文件管理", description = "文件列表查询、创建文件夹、重命名等功能")
public class AccountFileController {
    @Autowired
    private AccountFileService accountFileService;

    /**
     * 查询某个文件夹下的文件列表
     */
    @GetMapping("/list")
    @Operation(summary = "文件列表", description = "根据父ID获取当前目录下的文件列表")
    public JsonData list(@RequestParam(name = "parent_id",defaultValue = "0") Long parentId) {
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        List<AccountFileDTO> fileList =accountFileService.listFile(accountId,parentId);
        return JsonData.buildSuccess(fileList);
    }

    /**
     * 创建文件夹
     */
    @PostMapping("/create_folder")
    @Operation(summary = "创建文件夹", description = "根据请求参数在指定路径下创建新的文件夹")
    public JsonData createFolder(@RequestBody FolderCreateReq request) {

        accountFileService.createFolder(request);
        return JsonData.buildSuccess();
    }

    /**
     * 重命名
     */
    @PostMapping("/rename")
    @Operation(summary = "文件重命名", description = "修改指定文件或文件夹的名称")
    public JsonData renameFile(@RequestBody FileRenameReq request) {
        accountFileService.renameFile(request);
        return JsonData.buildSuccess();
    }

    /**
     * 查询文件树
     */
    @GetMapping("/folder/tree")
    @Operation(summary = "文件树", description = "获取当前用户的文件树结构")
    public JsonData folderTree(@RequestParam(name = "folder_id", defaultValue = "0")Long folderId) {
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        List<FolderTreeNodeDTO> list = accountFileService.folderTree(accountId,folderId);
        return JsonData.buildSuccess(list);
    }

    /**
     * 小文件上传
     */
    @Operation(summary = "小文件上传", description = "上传文件")
    @PostMapping("/upload")
    public JsonData upload(FileUploadReq request) {
        accountFileService.uploadFile(request);
        return JsonData.buildSuccess();
    }

    /**
     * 文件批量移动
     */
    @Operation(summary = "文件批量移动", description = "批量移动文件")
    @PostMapping("/move_batch")
    public JsonData moveBatch(@RequestBody FileBatchReq request) {
        accountFileService.moveBatch(request);
        return JsonData.buildSuccess();
    }


    /**
     * 批量删除
     */
    @Operation(summary = "批量删除", description = "批量删除文件")
    @PostMapping("/del_batch")
    public JsonData delBatch(@RequestBody FileBatchReq request) {
        accountFileService.delBatch(request);
        return JsonData.buildSuccess();
    }

    /**
     * 批量复制
     */
    @Operation(summary = "批量复制", description = "批量复制文件")
    @PostMapping("/copy_batch")
    public JsonData copyBatch(@RequestBody FileBatchReq request) {
        accountFileService.copyBatch(request);
        return JsonData.buildSuccess();
    }

}
